<?php

namespace App\Http\Controllers\meizu;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use DB;
class userController extends Controller
{
    //
	public function code()
	{
		ob_end_flush();
		$builder = new CaptchaBuilder;
		$builder->build();

		$builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 85, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        Session::flash('code', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
	}
	public function add(Request $request)
	{
		// dd($request -> all());


		$data = $request -> except('_token','acceptFlyme','vCodeTypeValue','vcode');
		$vcode = strtoupper($request['vcode']);
		$code = strtoupper(Session::get('code'));


		if($vcode == $code)
		{
			$res = DB::table('user') -> insert($data);
			if($res)
			{
				$data = DB::table('user') -> where('name',$request['name'])->first();
				DB::table('userdetail') -> insert(['nickname' => $data -> name]);

				//设置用户登录session
				session(['user' => $data -> id]);


				return redirect('/personal');
			}
			return back() -> with(['info1' => '账户名已存在!']);
		}
		return back() -> with(['info2' => '验证码错误!']);
	}
	
	public function detail(Request $request)
	{
		// dd($request -> all());
		// dd($data -> name);

		$data = DB::table('user') -> where('name',$request['name'])->first();
		$pass = $data -> password;
		if($pass == $request['password'])
		{
			
			session(['user' => $data -> id]);
			return redirect('/personal');

		}
		return back();
	}
	public function personal()
	{
		if((session() -> has('user'))&&( !empty(session('user'))))
        {
             
			$id = session('user');
			$data = DB::table('user') -> where('id',$id)->first();
			$detail = DB::table('userdetail') -> where('id',$id) ->first();

			return view('meizu.detail',['data' => $data,'detail' => $detail]);
        }
        back();
	}
	public function logout()
	{
		 session(['user' => '']);
		return redirect('/login');
	}

	public function order()
	{
		 // 上导航栏1表示
        $top = 1;
        $zoneT = DB::table('zone') ->where('place',$top) ->  get();
        // 获取上导航栏商品
        foreach($zoneT as $k => $v)
        {
            $goodsT[$k] = DB::table('goods') 
                        -> where('pid',$v -> pid) 
                        -> where('new','<>',1) 
                        -> Where('key',$v -> key)
                        -> get();
        }
          if((session() -> has('user'))&&( !empty(session('user'))))
        {
             
            $id = session('user');
            $data = DB::table('user') -> where('id',$id)->first();
            $detail = DB::table('userdetail') -> where('id',$id) ->first();

       
        }else{
            $data = null;
            $detail = null;
        }


        $order = DB::table('order') -> where('uid',$id) -> orderBy('time','desc') ->get();

        foreach($order as $k => $v)
        {

        	$v -> time = date('Y-m-d H:i:s',$v -> time); 
        }
        
	
       return view('meizu.myorder',
            ['zoneT'   => $zoneT,
            'goodsT'   => $goodsT,

            'order'    => $order,
             //用户信息
            'data'     => $data,
            'detail'   => $detail,
            ]);
	}

	public function pay($id)
	{
		   if((session() -> has('user'))&&( !empty(session('user'))))
        {
             
            $uid = session('user');
            $order = DB::table('user') -> where('id',$uid)->first();
       
        }
        $arr = explode('-',$order -> order);
        unset($arr[array_search($id,$arr)]);

        $str = implode('-',$arr);
        // dd($str);
        
        DB::table('user') -> where('id',$uid) -> update(['order' => $str]);

		DB::table('order') -> where('id',$id) ->  delete();
		return back();
	}

	public function phone(request $request)
	{
		$data = $request -> except('_token');
		$uid = session('user');
		DB::table('userdetail') -> where('id',$uid) -> update($data);
		return back();

	}
	public function email(request $request)
	{
		$data = $request -> except('_token');
		$uid = session('user');
		DB::table('user') -> where('id',$uid) -> update($data);
		return back();

	}
	public function password(request $request)
	{
		
		$data = $request -> except('_token');

		if(!empty($data['password'])&& (!empty($data['password1'])) )
		{
			
			
			$uid = session('user');
			$user = DB::table('user') -> where('id',$uid) -> first();
		
			if($user -> password == $data['password1'])
			{
				unset($data['password1']);
				
				DB::table('user') -> where('id',$uid) -> update($data);
			}
		}

		return back();

	}
	public function nickname(request $request)
	{
		$data = $request -> except('_token');

		$uid = session('user');
		DB::table('userdetail') -> where('id',$uid) -> update($data);
		return back();
	}
	public function pic(request $request)
	{


		$data = $request -> except('_token');

		$uid = session('user');
		$userpic = DB::table('user') -> select('pic') -> where('id',$uid)->first();
		if($request ->hasFile('pic'))
		{
			if($request -> file('pic') -> isvalid())
			{
				$suffix = $request -> file('pic') -> getclientOriginalExtension();
				$fileName = time().mt_rand(10000,99999).'.'.$suffix;
				$move = $request -> file('pic') -> move('./uploads/yhtx',$fileName);
				if($move)
				{

					$data['pic'] = $fileName;
					if(!empty($userpic -> pic))
					{
						unlink($userpic-> pic);
					}
					$res = DB::table('user') -> where('id',$uid) ->update(['pic'=> 'uploads/yhtx/'.$fileName]) ;
				}
			}
		}
		return back();
	}
}
